package ynu.edu.user.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import ynu.edu.common.result.Result;
import ynu.edu.user.dto.UserDTO;
import ynu.edu.user.dto.UserLoginDTO;
import ynu.edu.user.service.UserService;
import ynu.edu.user.vo.UserVO;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<UserVO> register(@RequestBody @Valid UserDTO userDTO) {
        UserVO userVO = userService.register(userDTO);
        return Result.success(userVO);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<String> login(@RequestBody @Valid UserLoginDTO loginDTO) {
        String token = userService.login(loginDTO);
        return Result.success(token);
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/{id}")
    public Result<UserVO> getUserInfo(@PathVariable Long id) {
        UserVO userVO = userService.getUserInfo(id);
        return Result.success(userVO);
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/{id}")
    public Result<UserVO> updateUserInfo(@PathVariable Long id, @RequestBody @Valid UserDTO userDTO) {
        userDTO.setId(id);
        UserVO userVO = userService.updateUserInfo(userDTO);
        return Result.success(userVO);
    }

    /**
     * 修改密码
     */
    @PutMapping("/{id}/password")
    public Result<Boolean> updatePassword(@PathVariable Long id,
                                         @RequestParam String oldPassword,
                                         @RequestParam String newPassword) {
        boolean result = userService.updatePassword(id, oldPassword, newPassword);
        return Result.success(result);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteUser(@PathVariable Long id) {
        boolean result = userService.deleteUser(id);
        return Result.success(result);
    }
    
    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/current")
    public Result<UserVO> getCurrentUserInfo(HttpServletRequest request) {
        // 从请求属性中获取用户ID
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            return Result.unauthorized();
        }
        
        // 获取用户信息
        UserVO userVO = userService.getUserInfo(userId);
        return Result.success(userVO);
    }
    
    /**
     * 通过手机号查询用户
     */
    @GetMapping("/phone/{phone}")
    public Result<UserVO> getUserByPhone(@PathVariable String phone) {
        UserVO userVO = userService.getUserByPhone(phone);
        return Result.success(userVO);
    }
    
    /**
     * 通过邮箱查询用户
     */
    @GetMapping("/email/{email}")
    public Result<UserVO> getUserByEmail(@PathVariable String email) {
        UserVO userVO = userService.getUserByEmail(email);
        return Result.success(userVO);
    }
    
    /**
     * 更新用户状态
     */
    @PutMapping("/{id}/status")
    public Result<Boolean> updateUserStatus(@PathVariable Long id, @RequestParam Integer status) {
        boolean result = userService.updateUserStatus(id, status);
        return Result.success(result);
    }
} 